package com.dz.lm.web.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.util.FileUploadUtil;
import com.dz.lm.common.utils.json.JsonUtils;
import com.dz.lm.service.manager.ManagerService;
import com.dz.lm.web.controller.BasicControl;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

@RestController
@RequestMapping(value = "/rewriteUrl/mine", produces = "application/json")
public class MineController extends BasicControl {

	public Logger logger = LogManager.getLogger(MineController.class);
	
    @Autowired
    private ManagerService managerService;
    
    /**
     * 获取我创建的或我代理的产品列表
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/qryMyProductList")
    public Map<String, Object> qryMyProductList(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = null;
    	//参数校验
		Vector<String> inparam = new Vector<String>();
		inparam.add("position");
		inparam.add("size");
		inparam.add("type");
		for (String in : inparam) {
			if (!_reqData.containsKey(in)) {
				subjo = new JSONObject();
		    	subjo.put("result", "01");
		    	subjo.put("message", "参数" + in + "异常");
		    	return parameterEncrypt(request, subjo);
			}
		}
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		String position = param.get("position") != null? param.get("position").toString() : "0";
		String size = param.get("size") != null? param.get("size").toString() : "10";		
		param.put("position", Integer.valueOf(position) * Integer.valueOf(size));
		param.put("size", Integer.valueOf(size));
		if (param.get("userId") == null || "".equals(param.get("userId"))) {
			param.put("userId", request.getAttribute("userId"));
		} else if(!param.get("userId").equals(request.getAttribute("userId").toString())) {
			param.put("upDownFlag", "0");
		}
		Map<String, Object> retData = managerService.qryMyProductList(param);
		subjo = new JSONObject();
		subjo.put("result", "00");
    	subjo.put("message", "SUCCESS");
    	subjo.put("productList", retData.get("productList"));
		int count = (int) retData.get("count");
		int page = 0;
		if (count % Integer.valueOf(size) > 0) {
			page = count / Integer.valueOf(size) + 1;
		} else {
			page = count / Integer.valueOf(size);
		}
    	subjo.put("count", count);
    	subjo.put("page", page);
		return parameterEncrypt(request, subjo);
    }

    /**
     * 获取我的代理人下【代理产品】的信息列表接口
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/qryUserAgentProductList")
    public Map<String, Object> qryUserAgentProductList(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = null;
    	//参数校验
		Vector<String> inparam = new Vector<String>();
		inparam.add("position");
		inparam.add("size");
		inparam.add("agentId");
		for (String in : inparam) {
			if (!_reqData.containsKey(in)) {
				subjo = new JSONObject();
		    	subjo.put("result", "01");
		    	subjo.put("message", "参数" + in + "异常");
		    	return parameterEncrypt(request, subjo);
			}
		}
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		String position = param.get("position") != null? param.get("position").toString() : "0";
		String size = param.get("size") != null? param.get("size").toString() : "10";		
		param.put("position", Integer.valueOf(position) * Integer.valueOf(size));
		param.put("size", Integer.valueOf(size));
		param.put("userId", request.getAttribute("userId"));
		Map<String, Object> retData = managerService.qryUserAgentProductList(param);
		subjo = new JSONObject();
		subjo.put("result", "00");
    	subjo.put("message", "SUCCESS");
    	subjo.put("productList", retData.get("productList"));
		int count = (int) retData.get("count");
		int page = 0;
		if (count % Integer.valueOf(size) > 0) {
			page = count / Integer.valueOf(size) + 1;
		} else {
			page = count / Integer.valueOf(size);
		}
    	subjo.put("count", count);
    	subjo.put("page", page);
		return parameterEncrypt(request, subjo);
    }
    
    /**
     * 获取我的代理人下【累计推送】信息列表接口
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/qryUserAgentProductApplyList")
    public Map<String, Object> qryUserAgentProductApplyList(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = null;
    	//参数校验
		Vector<String> inparam = new Vector<String>();
		inparam.add("position");
		inparam.add("size");
		inparam.add("agentId");
		for (String in : inparam) {
			if (!_reqData.containsKey(in)) {
				subjo = new JSONObject();
		    	subjo.put("result", "01");
		    	subjo.put("message", "参数" + in + "异常");
		    	return parameterEncrypt(request, subjo);
			}
		}
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		String position = param.get("position") != null? param.get("position").toString() : "0";
		String size = param.get("size") != null? param.get("size").toString() : "10";		
		param.put("position", Integer.valueOf(position) * Integer.valueOf(size));
		param.put("size", Integer.valueOf(size));
		param.put("userId", request.getAttribute("userId"));
		Map<String, Object> retData = managerService.qryUserAgentProductApplyList(param);
		subjo = new JSONObject();
		subjo.put("result", "00");
    	subjo.put("message", "SUCCESS");
    	subjo.put("applyList", retData.get("applyList"));
		int count = (int) retData.get("count");
		int page = 0;
		if (count % Integer.valueOf(size) > 0) {
			page = count / Integer.valueOf(size) + 1;
		} else {
			page = count / Integer.valueOf(size);
		}
    	subjo.put("count", count);
    	subjo.put("page", page);
		return parameterEncrypt(request, subjo);
    }
    
    /**
     * 获取我的代理人列表接口
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/qryMyAgentList")
    public Map<String, Object> qryMyAgentList(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = null;
    	//参数校验
		Vector<String> inparam = new Vector<String>();
		inparam.add("position");
		inparam.add("size");
		for (String in : inparam) {
			if (!_reqData.containsKey(in)) {
				subjo = new JSONObject();
		    	subjo.put("result", "01");
		    	subjo.put("message", "参数" + in + "异常");
		    	return parameterEncrypt(request, subjo);
			}
		}
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		String position = param.get("position") != null? param.get("position").toString() : "0";
		String size = param.get("size") != null? param.get("size").toString() : "10";		
		param.put("position", Integer.valueOf(position) * Integer.valueOf(size));
		param.put("size", Integer.valueOf(size));
		param.put("userId", request.getAttribute("userId"));
		Map<String, Object> retData = managerService.qryMyAgentList(param);
		subjo = new JSONObject();
		subjo.put("result", "00");
    	subjo.put("message", "SUCCESS");
    	subjo.put("agentList", retData.get("agentList"));
		int count = (int) retData.get("count");
		int page = 0;
		if (count % Integer.valueOf(size) > 0) {
			page = count / Integer.valueOf(size) + 1;
		} else {
			page = count / Integer.valueOf(size);
		}
    	subjo.put("count", count);
    	subjo.put("page", page);
		return parameterEncrypt(request, subjo);
    }
    
    /**
     * 获取我下级列表
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/qryLowLevelAgentList")
    public Map<String, Object> qryLowLevelAgentList(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = null;
    	//参数校验
		Vector<String> inparam = new Vector<String>();
		inparam.add("position");
		inparam.add("size");
		inparam.add("productId");
		for (String in : inparam) {
			if (!_reqData.containsKey(in)) {
				subjo = new JSONObject();
		    	subjo.put("result", "01");
		    	subjo.put("message", "参数" + in + "异常");
		    	return parameterEncrypt(request, subjo);
			}
		}
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		String position = param.get("position") != null? param.get("position").toString() : "0";
		String size = param.get("size") != null? param.get("size").toString() : "10";		
		param.put("position", Integer.valueOf(position) * Integer.valueOf(size));
		param.put("size", Integer.valueOf(size));
		if (param.get("userId") == null || "".equals(param.get("userId"))) {
			param.put("userId", request.getAttribute("userId"));
		}
		Map<String, Object> retData = managerService.qryLowLevelAgentList(param);
		subjo = new JSONObject();
		subjo.put("result", "00");
    	subjo.put("message", "SUCCESS");
    	subjo.put("totalApplyCount", retData.get("totalApplyCount"));
    	subjo.put("agentList", retData.get("agentList"));
		int count = (int) retData.get("count");
		int page = 0;
		if (count % Integer.valueOf(size) > 0) {
			page = count / Integer.valueOf(size) + 1;
		} else {
			page = count / Integer.valueOf(size);
		}
    	subjo.put("count", count);
    	subjo.put("page", page);
		return parameterEncrypt(request, subjo);
    }
    
    /**
     * 代理产品进件明细列表
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/qryAgentProductApplyList")
    public Map<String, Object> qryAgentProductApplyList(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = null;
    	//参数校验
		Vector<String> inparam = new Vector<String>();
		inparam.add("position");
		inparam.add("size");
		inparam.add("productId");
		for (String in : inparam) {
			if (!_reqData.containsKey(in)) {
				subjo = new JSONObject();
		    	subjo.put("result", "01");
		    	subjo.put("message", "参数" + in + "异常");
		    	return parameterEncrypt(request, subjo);
			}
		}
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		String position = param.get("position") != null? param.get("position").toString() : "0";
		String size = param.get("size") != null? param.get("size").toString() : "10";		
		param.put("position", Integer.valueOf(position) * Integer.valueOf(size));
		param.put("size", Integer.valueOf(size));
		if (param.get("userId") == null || "".equals(param.get("userId"))) {
			param.put("userId", request.getAttribute("userId"));
		}
		Map<String, Object> retData = managerService.qryAgentProductApplyList(param);
		subjo = new JSONObject();
		subjo.put("result", "00");
    	subjo.put("message", "SUCCESS");
    	subjo.put("applyList", retData.get("applyList"));
		int count = (int) retData.get("count");
		int page = 0;
		if (count % Integer.valueOf(size) > 0) {
			page = count / Integer.valueOf(size) + 1;
		} else {
			page = count / Integer.valueOf(size);
		}
    	subjo.put("count", count);
    	subjo.put("page", page);
		return parameterEncrypt(request, subjo);
    }
    
    /**
     * 查询我代理的产品的进件明细列表
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/qryMyAgentProductApplyList")
    public Map<String, Object> qryMyAgentProductApplyList(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = null;
    	//参数校验
		Vector<String> inparam = new Vector<String>();
		inparam.add("position");
		inparam.add("size");
		for (String in : inparam) {
			if (!_reqData.containsKey(in)) {
				subjo = new JSONObject();
		    	subjo.put("result", "01");
		    	subjo.put("message", "参数" + in + "异常");
		    	return parameterEncrypt(request, subjo);
			}
		}
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		String position = param.get("position") != null? param.get("position").toString() : "0";
		String size = param.get("size") != null? param.get("size").toString() : "10";		
		param.put("position", Integer.valueOf(position) * Integer.valueOf(size));
		param.put("size", Integer.valueOf(size));
		if (param.get("userId") == null || "".equals(param.get("userId"))) {
			param.put("userId", request.getAttribute("userId"));
		}
		Map<String, Object> retData = managerService.qryMyAgentProductApplyList(param);
		subjo = new JSONObject();
		subjo.put("result", "00");
    	subjo.put("message", "SUCCESS");
    	subjo.put("applyList", retData.get("applyList"));
		int count = (int) retData.get("count");
		int page = 0;
		if (count % Integer.valueOf(size) > 0) {
			page = count / Integer.valueOf(size) + 1;
		} else {
			page = count / Integer.valueOf(size);
		}
    	subjo.put("count", count);
    	subjo.put("page", page);
		return parameterEncrypt(request, subjo);
    }
    
    /**
     * 删除我创建的或我代理的产品
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/delMyProduct")
    public Map<String, Object> delMyProduct(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = new JSONObject();
    	//参数校验
		Vector<String> inparam = new Vector<String>();
		inparam.add("productId");
		inparam.add("type");
		for (String in : inparam) {
			if (!_reqData.containsKey(in)) {
		    	subjo.put("result", "01");
		    	subjo.put("message", "参数" + in + "异常");
		    	return parameterEncrypt(request, subjo);
			}
		}
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		String type = param.get("type") != null ? param.get("type").toString() : "1";
		String message = "";
		if ("1".equals(type)) {
			message = "产品";
		} else if ("2".equals(type)) {
			message = "代理产品";
		}
		param.put("userId", request.getAttribute("userId"));
		int count = managerService.delMyProduct(param);
		if (count == 1) {
			subjo.put("result", "00");
	    	subjo.put("message", message + "删除成功");
		} else {
			subjo.put("result", "01");
	    	subjo.put("message", message + "删除失败");
		}
		return parameterEncrypt(request, subjo);
    }
    
    /**
     * 获取创建的或代理的产品的申请信息列表
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/qryMyProductApplyList")
    public Map<String, Object> qryMyProductApplyList(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = null;
    	//参数校验
		Vector<String> inparam = new Vector<String>();
		inparam.add("position");
		inparam.add("size");
		inparam.add("type");
		for (String in : inparam) {
			if (!_reqData.containsKey(in)) {
				subjo = new JSONObject();
		    	subjo.put("result", "01");
		    	subjo.put("message", "参数" + in + "异常");
		    	return parameterEncrypt(request, subjo);
			}
		}
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		String position = param.get("position") != null? param.get("position").toString() : "0";
		String size = param.get("size") != null? param.get("size").toString() : "10";		
		param.put("position", Integer.valueOf(position) * Integer.valueOf(size));
		param.put("size", Integer.valueOf(size));
		param.put("userId", request.getAttribute("userId"));
		Map<String, Object> retData = managerService.qryMyProductApplyList(param);
		subjo = new JSONObject();
		subjo.put("result", "00");
    	subjo.put("message", "查询成功");
    	subjo.put("productApplyList", retData.get("productApplyList"));
		int count = (int) retData.get("count");
		int page = 0;
		if (count % Integer.valueOf(size) > 0) {
			page = count / Integer.valueOf(size) + 1;
		} else {
			page = count / Integer.valueOf(size);
		}
    	subjo.put("count", count);
    	subjo.put("page", page);
		return parameterEncrypt(request, subjo);
    }
    
    /**
     * 用户申请信息及借贷名片详情
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/qryApplyUserInfo")
    public Map<String, Object> qryApplyUserInfo(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = new JSONObject();;
    	//参数校验
		Vector<String> inparam = new Vector<String>();
		inparam.add("applyId");
		for (String in : inparam) {
			if (!_reqData.containsKey(in)) {
		    	subjo.put("result", "01");
		    	subjo.put("message", "参数" + in + "异常");
		    	return parameterEncrypt(request, subjo);
			}
		}
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		param.put("userId", request.getAttribute("userId"));
		Map<String, Object> retMap = managerService.qryApplyInfo(param);
		subjo.put("result", "00");
    	subjo.put("message", "查询成功");
    	subjo.put("applyInfo", retMap.get("applyInfo"));
    	subjo.put("creditCardInfo", retMap.get("creditCardInfo"));
		return parameterEncrypt(request, subjo);
    }
    
    /**
     * 申请记录的拒绝或确认接单
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/processApplyRecord")
    public Map<String, Object> processApplyRecord(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = new JSONObject();;
    	//参数校验
		Vector<String> inparam = new Vector<String>();
		inparam.add("applyId");
		inparam.add("state");
		for (String in : inparam) {
			if (!_reqData.containsKey(in)) {
		    	subjo.put("result", "01");
		    	subjo.put("message", "参数" + in + "异常");
		    	return parameterEncrypt(request, subjo);
			}
		}
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		param.put("userId", request.getAttribute("userId"));
		Map<String, Object> retData = managerService.processApplyRecord(param);
		subjo.put("result", retData.get("result"));
    	subjo.put("message", retData.get("message"));
    	subjo.put("orderInfo", retData.get("orderInfo"));
		return parameterEncrypt(request, subjo);
    }

    /**
     * 获取设置页内容
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/qryUserSetInfo")
    public Map<String, Object> qryUserSetInfo(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = new JSONObject();
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		param.put("userId", request.getAttribute("userId"));
		Map<String, Object> userInfo = managerService.qryUserSetInfo(param);
		if (userInfo != null) {
			subjo.put("result", "00");
	    	subjo.put("message", "查询成功");
	    	subjo.put("userInfo", userInfo);
		} else {
			subjo.put("result", "01");
	    	subjo.put("message", "未查的数据");
		}
		return parameterEncrypt(request, subjo);
    }
    
    /**
     * 设置页接口
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/userSet")
    public Map<String, Object> userSet(HttpServletRequest request) throws ServiceException {
    	Object imgUrl = request.getAttribute("imgUrl");
    	@SuppressWarnings("unchecked")
		Map<String, Object> images = (Map<String, Object>) request.getAttribute("images");
    	Map<String, Object> urlMap = new HashMap<String, Object>();
    	if (images != null && !images.isEmpty()) {
    		urlMap = FileUploadUtil.uploadByInStream(images, imgUrl, "profile");
    	}
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = new JSONObject();
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		//用户编码
		param.put("userId", request.getAttribute("userId"));
		Set<String> keys = urlMap.keySet();
		for (String key : keys) {
			param.put(key, imgUrl + urlMap.get(key).toString());
		}
		String password = param.get("password") != null ? param.get("password").toString() : null;
		String confirmPassword = param.get("confirmPassword") != null ? param.get("confirmPassword").toString() : null;
		//密码与确认密码进行比较
		if (password != null && !password.equals(confirmPassword)) {
	    	subjo.put("result", "02");
	    	subjo.put("message", "密码与确认密码不一致，请重新输入！");
	    	return parameterEncrypt(request, subjo);
		}
		int count = managerService.userSet(param);
		Map<String, Object> userInfo = managerService.qryUserSetInfo(param);
		if (count == 1) {
			//物理删除用户操作删除的图片
			deleteImages(param.get("deleteFiles"));
			subjo.put("result", "00");
	    	subjo.put("message", "修改成功");
	    	subjo.put("userInfo", userInfo);
		} else {
			subjo.put("result", "01");
	    	subjo.put("message", "修改失败");
		}
		return parameterEncrypt(request, subjo);
    }
    
    /**
     * 获取用户主页信息
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/qryUserHomeInfo")
    public Map<String, Object> qryUserHomeInfo(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = new JSONObject();
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		if (param.get("userId") == null || "".equals(param.get("userId"))) {
			param.put("userId", request.getAttribute("userId"));
		}
		param.put("activeUserId", request.getAttribute("userId"));
		Map<String, Object> data = managerService.qryUserHomeInfo(param);
		if (data != null && !data.isEmpty()) {
			subjo.put("result", "00");
	    	subjo.put("message", "查询成功");
	    	subjo.putAll(data);
		} else {
			subjo.put("result", "01");
	    	subjo.put("message", "未查的数据");
		}
		return parameterEncrypt(request, subjo);
    }
    
    /**
     * 获取用户创建的和代理的产品及需求信息
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/qryUserHomeProducts")
    public Map<String, Object> qryUserHomeProducts(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
		JSONObject _reqData = JSONObject.parseObject(reqData);
		JSONObject subjo = null;
		//参数校验
		Vector<String> inparam = new Vector<String>();
		inparam.add("position");
		inparam.add("size");
		for (String in : inparam) {
			if (!_reqData.containsKey(in)) {
				subjo = new JSONObject();
		    	subjo.put("result", "01");
		    	subjo.put("message", "参数" + in + "异常");
		    	return parameterEncrypt(request, subjo);
			}
		}
	
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		String position = param.get("position") != null? param.get("position").toString() : "0";
		String size = param.get("size") != null? param.get("size").toString() : "10";
		param.put("position", Integer.valueOf(position) * Integer.valueOf(size));
		param.put("size", Integer.valueOf(size));
		if (param.get("userId") == null || "".equals(param.get("userId"))) {
			param.put("userId", request.getAttribute("userId"));
		}
		param.put("activeUserId", request.getAttribute("userId"));
		Map<String, Object> retData = managerService.qryUserHomeProducts(param);
		subjo = new JSONObject();
		subjo.put("result", "00");
		subjo.put("message", "SUCCESS");
		subjo.put("productList", retData.get("productList"));
		int count = (int) retData.get("count");
		int page = 0;
		if (count % Integer.valueOf(size) > 0) {
			page = count / Integer.valueOf(size) + 1;
		} else {
			page = count / Integer.valueOf(size);
		}
		subjo.put("count", count);
		subjo.put("page", page);
		return parameterEncrypt(request, subjo);
    }
    
    /**
     * 获取经理人及机构认证信息
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/qryManageAuthInfo")
    public Map<String, Object> qryManageAuthInfo(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = new JSONObject();
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		if (param.get("userId") == null || "".equals(param.get("userId"))) {
			param.put("userId", request.getAttribute("userId"));
		}
		Map<String, Object> data = managerService.qryManageAuthInfo(param);
		if (data != null && !data.isEmpty()) {
			subjo.put("result", "00");
	    	subjo.put("message", "查询成功");
	    	subjo.putAll(data);
		} else {
			subjo.put("result", "01");
	    	subjo.put("message", "未查的数据");
		}
		return parameterEncrypt(request, subjo);
    }
    
    /**
     * 获取用户的历史评论列表
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/qryUserHomeComments")
    public Map<String, Object> qryUserHomeComments(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = new JSONObject();
		//参数校验
		Vector<String> inparam = new Vector<String>();
		inparam.add("position");
		inparam.add("size");
		for (String in : inparam) {
			if (!_reqData.containsKey(in)) {
				subjo = new JSONObject();
		    	subjo.put("result", "01");
		    	subjo.put("message", "参数" + in + "异常");
		    	return parameterEncrypt(request, subjo);
			}
		}
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		String position = param.get("position") != null? param.get("position").toString() : "0";
		String size = param.get("size") != null? param.get("size").toString() : "10";
		param.put("position", Integer.valueOf(position) * Integer.valueOf(size));
		param.put("size", Integer.valueOf(size));
		if (param.get("userId") == null || "".equals(param.get("userId"))) {
			param.put("userId", request.getAttribute("userId"));
		}
		Map<String, Object> retData = managerService.qryUserHomeComments(param);
		subjo = new JSONObject();
		subjo.put("result", "00");
		subjo.put("message", "查询成功");
		subjo.put("commentList", retData.get("commentList"));
		int count = (int) retData.get("count");
		int page = 0;
		if (count % Integer.valueOf(size) > 0) {
			page = count / Integer.valueOf(size) + 1;
		} else {
			page = count / Integer.valueOf(size);
		}
		subjo.put("count", count);
		subjo.put("page", page);
		return parameterEncrypt(request, subjo);
    }
    /**
     * 获取我的申请记录
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/qryMyApplyList")
    public Map<String, Object> qryMyApplyList(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = new JSONObject();
		//参数校验
		Vector<String> inparam = new Vector<String>();
		inparam.add("position");
		inparam.add("size");
		for (String in : inparam) {
			if (!_reqData.containsKey(in)) {
				subjo = new JSONObject();
		    	subjo.put("result", "01");
		    	subjo.put("message", "参数" + in + "异常");
		    	return parameterEncrypt(request, subjo);
			}
		}
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		String position = param.get("position") != null? param.get("position").toString() : "0";
		String size = param.get("size") != null? param.get("size").toString() : "10";
		param.put("position", Integer.valueOf(position) * Integer.valueOf(size));
		param.put("size", Integer.valueOf(size));
		if (param.get("userId") == null || "".equals(param.get("userId"))) {
			param.put("userId", request.getAttribute("userId"));
		}
		Map<String, Object> retData = managerService.qryMyApplyList(param);
		subjo = new JSONObject();
		subjo.put("result", "00");
		subjo.put("message", "查询成功");
		subjo.put("applyList", retData.get("applyList"));
		int count = (int) retData.get("count");
		int page = 0;
		if (count % Integer.valueOf(size) > 0) {
			page = count / Integer.valueOf(size) + 1;
		} else {
			page = count / Integer.valueOf(size);
		}
		subjo.put("count", count);
		subjo.put("page", page);
		return parameterEncrypt(request, subjo);
    }
    
    @ResponseBody
    @RequestMapping("/qry1LevelAgentProductList")
    public Map<String, Object> qry1LevelAgentProductList(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = new JSONObject();
		//参数校验
		Vector<String> inparam = new Vector<String>();
		inparam.add("position");
		inparam.add("size");
		for (String in : inparam) {
			if (!_reqData.containsKey(in)) {
				subjo = new JSONObject();
		    	subjo.put("result", "01");
		    	subjo.put("message", "参数" + in + "异常");
		    	return parameterEncrypt(request, subjo);
			}
		}
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		String position = param.get("position") != null? param.get("position").toString() : "0";
		String size = param.get("size") != null? param.get("size").toString() : "10";
		param.put("position", Integer.valueOf(position) * Integer.valueOf(size));
		param.put("size", Integer.valueOf(size));
		param.put("userId", request.getAttribute("userId"));
		Map<String, Object> retData = managerService.qry1LevelAgentProductList(param);
		subjo = new JSONObject();
		subjo.put("result", "00");
		subjo.put("message", "查询成功");
		subjo.put("applyList", retData.get("_1LevelAgentProductList"));
		int count = (int) retData.get("count");
		int page = 0;
		if (count % Integer.valueOf(size) > 0) {
			page = count / Integer.valueOf(size) + 1;
		} else {
			page = count / Integer.valueOf(size);
		}
		subjo.put("count", count);
		subjo.put("page", page);
		return parameterEncrypt(request, subjo);
    }
    
    @ResponseBody
    @RequestMapping("/add2LevelAgentProduct")
	public Map<String, Object> add2LevelAgentProduct(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
		JSONObject _reqData = JSONObject.parseObject(reqData);
		JSONObject subjo = new JSONObject();
		// 参数校验
		Vector<String> inparam = new Vector<String>();
		inparam.add("agentId");
		for (String in : inparam) {
			if (!_reqData.containsKey(in)) {
				subjo = new JSONObject();
				subjo.put("result", "01");
				subjo.put("message", "参数" + in + "异常");
				return parameterEncrypt(request, subjo);
			}
		}
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
    	if (param.get("userId") == null || "".equals(param.get("userId"))) {
			param.put("userId", request.getAttribute("userId"));
		}
    	
//    	Map<String, Object> param = new HashMap<String, Object>();
//    	JSONObject subjo = new JSONObject();
//    	param.put("userId","1");
//    	param.put("agentId","3");
    	
    	int count = 0;
		if (count > 0) {
			subjo.put("result", "02");
	    	subjo.put("message", "该产品您已申请了代理，请确认代理产品是否已发布");
		} else {
			count = managerService.add2LevelAgentProduct(param);
			if (count > 1) {
				subjo.put("result", "00");
				subjo.put("message", "申请专属代理成功");
				subjo.put("productId", count);
			} else {
				subjo.put("result", "01");
				subjo.put("message", "申请专属代理失败");
			}
		}
		return parameterEncrypt(request, subjo);
	}
    
    /**
     * 查看我的账户
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/qryMyAccountInfo")
    public Map<String, Object> qryMyAccountInfo(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = new JSONObject();
		//参数校验
		Vector<String> inparam = new Vector<String>();
		inparam.add("position");
		inparam.add("size");
		for (String in : inparam) {
			if (!_reqData.containsKey(in)) {
				subjo = new JSONObject();
		    	subjo.put("result", "01");
		    	subjo.put("message", "参数" + in + "异常");
		    	return parameterEncrypt(request, subjo);
			}
		}
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		String position = param.get("position") != null? param.get("position").toString() : "0";
		String size = param.get("size") != null? param.get("size").toString() : "10";
		param.put("position", Integer.valueOf(position) * Integer.valueOf(size));
		param.put("size", Integer.valueOf(size));
		if (param.get("userId") == null || "".equals(param.get("userId"))) {
			param.put("userId", request.getAttribute("userId"));
		}
		Map<String, Object> retData = managerService.qryMyAccountInfo(param);
		subjo = new JSONObject();
		subjo.put("result", "00");
		subjo.put("message", "查询成功");
		subjo.put("cashAmount", retData.get("cashAmount"));
		subjo.put("incomeAmount", retData.get("incomeAmount"));
		subjo.put("cashWithdrawal", retData.get("cashWithdrawal"));
		subjo.put("payAmount", retData.get("payAmount"));
		subjo.put("tradeList", retData.get("tradeList"));
		int count = (int) retData.get("count");
		int page = 0;
		if (count % Integer.valueOf(size) > 0) {
			page = count / Integer.valueOf(size) + 1;
		} else {
			page = count / Integer.valueOf(size);
		}
		subjo.put("count", count);
		subjo.put("page", page);
		return parameterEncrypt(request, subjo);
    }
}
